package com.jmb.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jmb.api.common.exception.LeaseException;
import com.jmb.api.common.result.ResultCodeEnum;
import com.jmb.api.common.utils.ValidUtil;
import com.jmb.api.model.entity.ApiCaseSuiteProcessor;
import com.jmb.api.model.to.api.suite.ApiCaseSuiteProcessorTO;
import com.jmb.api.model.vo.api.suite.ApiCaseSuiteProcessorVO;
import com.jmb.api.service.ApiCaseSuiteProcessorService;
import com.jmb.api.mapper.ApiCaseSuiteProcessorMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author jinbo
* @description 针对表【api_case_suite_processor】的数据库操作Service实现
* @createDate 2025-04-08 13:53:56
*/
@Service
public class ApiCaseSuiteProcessorServiceImpl extends ServiceImpl<ApiCaseSuiteProcessorMapper, ApiCaseSuiteProcessor>
    implements ApiCaseSuiteProcessorService{
    @Autowired
    private ApiCaseSuiteProcessorMapper apiCaseSuiteProcessorMapper;

    /**
     * 添加处理器
     * @param apiCaseSuiteProcessor 处理器
     * @return 自增
     */
    @Override
    public ApiCaseSuiteProcessor saveProcessor(ApiCaseSuiteProcessor apiCaseSuiteProcessor) {
        // 校验JSON格式
        JsonCheCk(apiCaseSuiteProcessor);
        // 插入单数据
        this.save(apiCaseSuiteProcessor);
        return apiCaseSuiteProcessor;
    }

    /**
     * 编辑处理器
     * @param apiCaseSuiteProcessor 处理器
     */
    @Override
    public void editSuitProcessor(ApiCaseSuiteProcessor apiCaseSuiteProcessor) {
        // 校验参数
        JsonCheCk(apiCaseSuiteProcessor);
        this.saveOrUpdate(apiCaseSuiteProcessor);

    }

    /**
     * 查询处理器列表
     * @param caseSuiteProcessorTO 查询条件
     * @return 返回处理器列表
     */
    @Override
    public List<ApiCaseSuiteProcessorVO> findAllApiCaseSuiteProcessorList(ApiCaseSuiteProcessorTO caseSuiteProcessorTO) {
        return apiCaseSuiteProcessorMapper.selectApiCaseSuiteProcessorList(caseSuiteProcessorTO);
    }

    /**
     * 分页查询
     * @param page 分页对象
     * @param caseSuiteProcessorTO 查询条件
     * @return 返回分页数据
     */
    @Override
    public IPage<ApiCaseSuiteProcessorVO> findApiCaseSuiteProcessorList(IPage<ApiCaseSuiteProcessorVO> page, ApiCaseSuiteProcessorTO caseSuiteProcessorTO) {
        return apiCaseSuiteProcessorMapper.selectApiCaseSuiteProcessorListPage(page,caseSuiteProcessorTO);
    }

    /**
     *
     * 根据ID查询
     * @param id 传入id对象
     * @return 返回查询的数据对象
     */
    @Override
    public ApiCaseSuiteProcessorVO findApiCaseSuiteProcessorById(Integer id) {
        if (id ==null){
            throw new LeaseException(ResultCodeEnum.WEB_API_ID_NOT_NULL);
        }
        LambdaQueryWrapper<ApiCaseSuiteProcessor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApiCaseSuiteProcessor::getId,id);
        ApiCaseSuiteProcessor processor = this.getOne(queryWrapper);
        ApiCaseSuiteProcessorVO processorVO = new ApiCaseSuiteProcessorVO();
        BeanUtils.copyProperties(processor,processorVO);
        return processorVO;
    }

    @Override
    public List<ApiCaseSuiteProcessorVO> findApiCaseSuiteProcessorBySuiteId(Integer suiteId) {
        if (suiteId ==null){
            throw new LeaseException(ResultCodeEnum.WEB_API_ID_NOT_NULL);
        }
        List<ApiCaseSuiteProcessor> processors = this.lambdaQuery().eq(ApiCaseSuiteProcessor::getSuiteId, suiteId).list();
        return processors.stream().map(processor -> {
            ApiCaseSuiteProcessorVO processorVO = new ApiCaseSuiteProcessorVO();
            BeanUtils.copyProperties(processor,processorVO);
            return processorVO;
        }).toList();
    }

    @Override
    public void removeApiCaseSuiteProcessorById(Integer id) {
        if (id ==null){
            throw new LeaseException(ResultCodeEnum.WEB_API_ID_NOT_NULL);
        }
        this.removeById(id);
    }

    @Override
    public void removeApiCaseSuiteProcessorBySuiteId(Integer suiteId) {
        if (suiteId ==null){
            throw new LeaseException(ResultCodeEnum.WEB_API_ID_NOT_NULL);
        }
        LambdaQueryWrapper<ApiCaseSuiteProcessor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApiCaseSuiteProcessor::getSuiteId,suiteId);
        this.remove(queryWrapper);
    }
    public void JsonCheCk(ApiCaseSuiteProcessor apiCaseSuiteProcessor){
        if (apiCaseSuiteProcessor.getType()==1){
            ValidUtil.isJsonObject(apiCaseSuiteProcessor.getValue());
        }else if (apiCaseSuiteProcessor.getType()==2){
            ValidUtil.isJsonObject(apiCaseSuiteProcessor.getValue());
        }else if (apiCaseSuiteProcessor.getType()==3){
            ValidUtil.isJsonObject(apiCaseSuiteProcessor.getValue());
        }
    }

}




